package utils

import (
	"encoding/json"
	"fmt"

	"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

type DescribeInstances struct {
	Instances struct {
		Instance []ecs.Instance `json:"Instance"`
	} `json:"Instances"`
	TotalCount int    `json:"TotalCount"`
	PageSize   int    `json:"PageSize"`
	RequestID  string `json:"RequestId"`
	PageNumber int    `json:"PageNumber"`
}

func GetEcsInfo(RegionId string, AccessKeyId string, AccessKeySecret string) (DescribeInstances, error) {
	var respBody DescribeInstances

	// 创建ecsClient实例
	ecsClient, _ := ecs.NewClientWithAccessKey(RegionId, AccessKeyId, AccessKeySecret)

	// 查询一台或多台ECS实例的详细信息
	req := ecs.CreateDescribeInstancesRequest()

	// 设置请求协议
	req.Scheme = "https"

	// 通过ecsClient发起请求
	resp, _ := ecsClient.DescribeInstances(req)

	if code := resp.GetHttpStatus(); code != 200 {
		return respBody, fmt.Errorf("阿里云接口请求失败，状态码：%d", code)
	}

	// 解析json
	_ = json.Unmarshal([]byte(resp.GetHttpContentString()), &respBody)

	return respBody, nil
}
